package com.hss.sort;

public class HeapSort2 {
    public static void main(String[] args) {
        int[] a = new Data().a;
        sort(a);
        Print.printArr(a);
    }

    static void sort(int[] arr){
        int len = arr.length;
        for (int x = (int) Math.floor(len/2); x>=0; x--){
            heapify(arr,x,len);
        }

        for (int i=len-1;i>0;i--){
            swap(arr,0,i);
            heapify(arr,0,i);
        }



    }



    static void heapify(int arr[], int index, int len){
        int left = index*2 +1;
        int right = index * 2 +2;
        int max = index;
        if (left < len){
            if (arr[left] >arr[max]){
                max= left;
            }
        }
        if (right < len){
            if (arr[right] > arr[max]){
                max = right;
            }
        }
        if (max != index){
            swap(arr,max,index);

            heapify(arr,max,len);
        }
    }

    public static void swap(int[]arr, int i, int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
